package cate.game.interact;

import cate.common.rpc.path.RpcCommonPath;
import cate.common.table.d.GDFunc;
import cate.game.display.cmd.WatchHeroCmd;
import cate.game.display.msg.HeroDisplayGetResp;
import cate.game.display.po.HeroDisplay;
import cate.game.framework.ModuleMan;
import cate.game.play.part.PlaySide;
import cate.game.role.Role;
import cate.game.role.farm.pk.cmd.PkCmd;
import cate.game.role.show.cmd.WatchBlogCmd;
import cate.game.role.show.msg.OtherRoleBlogResp;
import cp.solution.rpc.RpcMsgHead;
import easy.java.practice.errorcode.EcResult;

import java.util.Optional;

/** 交互管理 */
public class InteractManager extends ModuleMan {

    @Override
    public void initDependent() throws Exception {
        game.rpcRecv().onRequestResponseTemplate(RpcCommonPath.WATCH_BLOG_G2G, WatchBlogCmd.class, this::onWatchBlogHandler);
        game.rpcRecv().onRequestResponseTemplate(RpcCommonPath.ROLE_PK_G2G, PkCmd.class, this::onRolePkHandler);
        game.rpcRecv().onRequestResponseTemplate(RpcCommonPath.WATCH_HERO_G2G, WatchHeroCmd.class, this::onWatchHeroHandler);
    }

    private WatchBlogCmd onWatchBlogHandler(RpcMsgHead head, WatchBlogCmd cmd) {
        Role tar = game.role.getRole(cmd.roleId, true);
        cmd.data = new OtherRoleBlogResp(tar);
        return cmd;
    }

    private PkCmd onRolePkHandler(RpcMsgHead head, PkCmd cmd) {
        Role tar = game.role.getRole(cmd.roleId, true);
        EcResult<PlaySide> r = game.fight.side.createPlaySideByRole(tar, GDFunc.ADVENTURE);
        if (r.ok()) {
            cmd.data = r.data;
        }
        return cmd;
    }

    private WatchHeroCmd onWatchHeroHandler(RpcMsgHead head, WatchHeroCmd cmd) {
        Optional<HeroDisplay> opt = game.facilities.getMongo().heroDisplay.findById(cmd.displayUid);
        if (opt.isPresent()) {
            HeroDisplayGetResp resp = new HeroDisplayGetResp();
            resp.display = opt.get();
            cmd.data = resp;
        }
        return cmd;
    }
}

